package com.sy.simpleegg.room.repository

import com.sy.simpleegg.room.dao.CoinDao
import com.sy.simpleegg.room.entity.CoinEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class CoinRepository(private val coinDao: CoinDao) {

    suspend fun getAllCoin(): MutableList<CoinEntity>? {
        return withContext(Dispatchers.IO) {
            coinDao.getAll()
        }
    }

    suspend fun getLastCoin(): MutableList<CoinEntity>? {
        return withContext(Dispatchers.IO) {
            coinDao.getLastCoins()
        }
    }

    suspend fun getClearData(startDate: String, endDate: String): MutableList<CoinEntity>? {
        return withContext(Dispatchers.IO) {
            coinDao.getClearCoins(startDate, endDate)
        }
    }

    suspend fun getByDate(date: String): MutableList<CoinEntity>? {
        return withContext(Dispatchers.IO) {
            coinDao.getByDate(date)
        }
    }

    suspend fun update(coinEntity: CoinEntity) {
        withContext(Dispatchers.IO) {
            coinDao.update(coinEntity)
        }
    }

    suspend fun insert(coinEntity: CoinEntity) {
        withContext(Dispatchers.IO) {
            coinDao.insertTransation(coinEntity)
        }
    }

    suspend fun insertAll(coinEntity: MutableList<CoinEntity>) {
        withContext(Dispatchers.IO) {
            coinDao.insertAll(coinEntity)
        }
    }

    suspend fun delete(coinEntity: CoinEntity) {
        withContext(Dispatchers.IO) {
            coinDao.delete(coinEntity)
        }
    }

    suspend fun deleteAll() {
        withContext(Dispatchers.IO) {
            coinDao.deleteAll()
        }
    }
}